package com.example.userserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.commons.entity.SysBrandDO;
import com.example.commons.utils.ApiResult;
import com.example.commons.utils.JWTUtil;
import com.example.userserver.entity.vo.SysBrandVO;
import com.example.userserver.service.SysBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.userserver.util.AMapUtils.addressToGPS;

@RestController
@RequestMapping("/jinZhiIndex/brand")
public class BrandController {
    @Autowired
    private SysBrandService sysBrandService;

    /**
     * 品牌申请接口
     * @param sysBrandDO
     * @return
     */
    @PostMapping("/brandSave")
    public  ApiResult<?> brandSave(SysBrandDO sysBrandDO) {

        QueryWrapper<SysBrandDO> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",JWTUtil.getUser().getId());
        // wrapper.eq("user_id",2);
        List<SysBrandDO> list= sysBrandService.list(wrapper);
        if(list.size()>0){
            return ApiResult.fail("请勿重复申请！");
        }
        sysBrandDO.setCreateTime(LocalDateTime.now());
        sysBrandDO.setUserId(JWTUtil.getUser().getId());
        sysBrandDO.setBrandType(sysBrandDO.getBrandType());//品牌类型
        sysBrandDO.setStatus(0);
        sysBrandDO.setState(0);
      boolean b=  sysBrandService.save(sysBrandDO);


        return b==true? ApiResult.success():ApiResult.fail();
    }


    /**
     * 品牌修改接口
     * @param sysBrandDO
     * @return
     */
    @PostMapping("/brandUp")
    public  ApiResult<?> brandUp(SysBrandDO sysBrandDO) {
        if(sysBrandDO.getId()==null){
            return ApiResult.fail("缺少参数");
        }
        sysBrandDO.setUpdateTime(LocalDateTime.now());
        sysBrandDO.setStatus(0);
        boolean b=  sysBrandService.updateById(sysBrandDO);

        return b==true? ApiResult.success():ApiResult.fail();
    }


    /**
     * 品牌修改接口
     * @return
     */
    @PostMapping("/result")
    public  ApiResult<?> result() {
        QueryWrapper<SysBrandDO> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",JWTUtil.getUser().getId());
        // wrapper.eq("user_id",2);
      List<SysBrandDO> list= sysBrandService.list(wrapper);
      //  System.out.println("集合长度=="+list.size());
        Map map=new HashMap();
        if(list.size()==0){
            //未申请机构
            map.put("code",3);
            map.put("msg","未申请机构");
            map.put("brandName"," ");
            map.put("createTime"," ");
         //   System.out.println("未进行申请=="+list.size());

            return ApiResult.success(map);
        }
        if(list.get(0).getStatus()==0){
            //申请机构成功
            map.put("code",0);
            map.put("msg","待审核");
            map.put("brandName",list.get(0).getBrandName());
            map.put("createTime",list.get(0).getCreateTime());
            return ApiResult.success(map);
        }
        if(list.get(0).getStatus()==1){
            //申请机构成功
            map.put("code",1);
            map.put("msg","已申请机构");
            map.put("brandName",list.get(0).getBrandName());
            map.put("createTime",list.get(0).getCreateTime());
            return ApiResult.success(map);
        }else {
            //申请机构失败
            map.put("code",2);
            map.put("msg","申请机构失败");
            map.put("brandName",list.get(0).getBrandName());
            map.put("createTime",list.get(0).getCreateTime());
            return ApiResult.success(map);
        }

       // return ApiResult.success(map);
    }


    /**
     * 品牌列表查询
     * @param current
     * @param number
     * @param sysBrandVO
     * @return
     */
    @GetMapping("/selBrand")
    public ApiResult<?> selBrand(int current, int number,SysBrandVO sysBrandVO){
       return ApiResult.success(sysBrandService.selBrand( current, number,sysBrandVO));
    }

    /**
     * 品牌详情查询
     * @return
     */
    @GetMapping("/selBrandById")
    public ApiResult<?> selBrandById(String id){
        SysBrandDO sysBrandDO = sysBrandService.selBrandById(id);
        sysBrandDO.setBrandImg(imgFromat(sysBrandDO.getBrandImg()));
        //全地址
        String address = sysBrandDO.getWorkAddress() + sysBrandDO.getDetailsAddress();
        double[] doubles = addressToGPS(address);
        if(doubles!=null&&doubles.length>0){
            sysBrandDO.setLatitude(doubles[1]);
            sysBrandDO.setLongitude(doubles[0]);
        }else {
            sysBrandDO.setLatitude(112.98626);
            sysBrandDO.setLongitude(28.25591);
        }

        return ApiResult.success(sysBrandDO);

    }



    private String imgFromat(String img){
        if (img == null&&"".equals(img)){
            return "";
        }
        int length = img.length();
        String dh = img.substring(length-1);
        if (",".equals(dh)){
            return img.substring(0,length-1);
        }
        return img;
    }


}
